from google.appengine.ext import webapp
from google.appengine.api import users
from google.appengine.ext import db
import logging
from punch import PunchEntity

class ViewXML(webapp.RequestHandler):
    def get(self):
        # Gather desired entities
        current_employee = users.GetCurrentUser()
        punch_entity_query = db.GqlQuery("SELECT * FROM PunchEntity WHERE employee = :1 "
                                   "ORDER BY punch_date DESC",
                                   current_employee)
        # Write XML
        self.response.out.write('<result>')
        for result in punch_entity_query:
            self.response.out.write('<punch_entity>')
            self.response.out.write('<punch_date>' + str(result.punch_date) + '</punch_date>')
            self.response.out.write('<hours>' + str(result.hours) + '</hours>')
            self.response.out.write('<project>' + result.project + '</project>')
            self.response.out.write('<summary>' + result.summary + '</summary>')
            self.response.out.write('</punch_entity>')
        self.response.out.write('</result>') 

        # Write Headers
        self.response.headers['Content-Type'] = 'text/xml'

class ViewHTML(webapp.RequestHandler):
    def get(self):
        # Gather desired entities
        current_employee = users.GetCurrentUser()
        punch_entity_query = db.GqlQuery("SELECT * FROM PunchEntity WHERE employee = :1 "
                                   "ORDER BY punch_date DESC",
                                   current_employee)
        # Write HTML
        self.response.out.write('<html>')
        self.response.out.write('<h2>' + str(current_employee.nickname()) + "'s Punchcard</h2><pre><!-- to preserve whitespace-->")
        for result in punch_entity_query:
            self.response.out.write('<b>Date: </b>' + str(result.punch_date) + '<br />')
            self.response.out.write('    <b>project: </b>' + result.project + '<br />')
            self.response.out.write('      <b>Hours: </b>' +str(result.hours) + '<br />')
            self.response.out.write('    <b>Summary: </b>' + result.summary + '<br /><br />')
        self.response.out.write('</pre>') # There has to be a better way to indent project, hours, and summary

        # Write Headers
        self.response.headers['Content-Type'] = 'text/html'
